<!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>&#x955C;&#x5B50;&#x53CD;&#x5C04;</title>
        <style>
</style>
        
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Microsoft/vscode/extensions/markdown-language-features/media/markdown.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Microsoft/vscode/extensions/markdown-language-features/media/highlight.css">
<style>
            body {
                font-family: -apple-system, BlinkMacSystemFont, 'Segoe WPC', 'Segoe UI', system-ui, 'Ubuntu', 'Droid Sans', sans-serif;
                font-size: 14px;
                line-height: 1.6;
            }
        </style>
        <style>
.task-list-item { list-style-type: none; } .task-list-item-checkbox { margin-left: -20px; vertical-align: middle; }
</style>
        
        
        
    </head>
    <body class="vscode-body vscode-light">
        <h1 id="镜子反射">镜子反射</h1>
<h1 id="1-lc-858-mirror-reflection">1. LC 858. Mirror Reflection</h1>
<ul>
<li><a href="https://leetcode.com/problems/mirror-reflection/">https://leetcode.com/problems/mirror-reflection/</a></li>
</ul>
<p>There is a special square room with mirrors on each of the four walls.  Except for the southwest corner, there are receptors on each of the remaining corners, numbered 0, 1, and 2.</p>
<p>The square room has walls of length p, and a laser ray from the southwest corner first meets the east wall at a distance q from the 0th receptor.</p>
<p>Return the number of the receptor that the ray meets first.  (It is guaranteed that the ray will meet a receptor eventually.)</p>
<p><img src="pics/mirror0.png" alt="mirror0.png"></p>
<pre><code><code><div>Example 1:

Input: p = 2, q = 1
Output: 2
Explanation: The ray meets receptor 2 the first time it gets reflected back to the left wall.
</div></code></code></pre>
<p>Note:</p>
<p>1 &lt;= p &lt;= 1000</p>
<p>0 &lt;= q &lt;= p</p>
<p>参考答案</p>
<ul>
<li><a href="https://leetcode.com/problems/mirror-reflection/discuss/146336/Java-solution-with-an-easy-to-understand-explanation">https://leetcode.com/problems/mirror-reflection/discuss/146336/Java-solution-with-an-easy-to-understand-explanation</a></li>
</ul>
<pre><code class="language-java"><div> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">mirrorReflection</span><span class="hljs-params">(<span class="hljs-keyword">int</span> p, <span class="hljs-keyword">int</span> q)</span> </span>{
        <span class="hljs-keyword">int</span> m = <span class="hljs-number">1</span>, n = <span class="hljs-number">1</span>;
        <span class="hljs-keyword">while</span>(m * p != n * q){
            n++;
            m = n * q / p;
        }
        <span class="hljs-keyword">if</span> (m % <span class="hljs-number">2</span> == <span class="hljs-number">0</span> &amp;&amp; n % <span class="hljs-number">2</span> == <span class="hljs-number">1</span>) <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
        <span class="hljs-keyword">if</span> (m % <span class="hljs-number">2</span> == <span class="hljs-number">1</span> &amp;&amp; n % <span class="hljs-number">2</span> == <span class="hljs-number">1</span>) <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
        <span class="hljs-keyword">if</span> (m % <span class="hljs-number">2</span> == <span class="hljs-number">1</span> &amp;&amp; n % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>) <span class="hljs-keyword">return</span> <span class="hljs-number">2</span>;
        <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;
    }
</div></code></pre>
<p>First, think about the case p = 3 &amp; q = 2.</p>
<p><img src="pics/mirror.png" alt="mirror.png"></p>
<p>So, this problem can be transformed into finding m * p = n * q, where</p>
<ul>
<li>m = the number of room extension + 1.</li>
<li>n = the number of light reflection + 1.</li>
</ul>
<p>经过分析</p>
<ul>
<li>If the number of light reflection is odd (which means n is even), it means the corner is on the left-hand side. The possible corner is 2.
Otherwise, the corner is on the right-hand side. The possible corners are 0 and 1.</li>
<li>Given the corner is on the right-hand side.
If the number of room extension is even (which means m is odd), it means the corner is 1. Otherwise, the corner is 0.</li>
</ul>
<p>So, we can conclude:</p>
<ul>
<li>m is even &amp; n is odd =&gt; return 0.</li>
<li>m is odd &amp; n is odd =&gt; return 1.</li>
<li>m is odd &amp; n is even =&gt; return 2.</li>
</ul>

    </body>
    </html>